Simple Socket ServerΒΆ

Publish random line from file to port 9000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
"""
import os
import random, time
import socketserver

class hwRequestHandler(socketserver.StreamRequestHandler):
    """ The request handler class for socket server.
        It is instantiated once per connection to the server,
        and must override the handle() method to implement
        communication to the client.
    """

    def get_random_line(self, fpath):
        with open(fpath, 'r', encoding="utf-8") as fh:
            lines = fh.readlines()
        return random.choice(lines)

    def handle(self):
        # self.rfile is a file-like object created by the handler;
        # we can now use e.g. readline() instead of raw recv() calls

        out_str = "Client on {}".format(self.client_address[0])
        self.wfile.write(out_str.encode('utf-8'))

        file_path = os.path.join("/home/cloudops/spark", "streamingtweets.txt")
        while True:
            # data = conn.recv(1024).decode() # receive data stream
            # if not data:  break
            # Pick a random line from a list
            self.data = self.get_random_line(file_path)
            # Publish the line
            print("Publishing: " + self.data);
            self.wfile.write(self.data.encode('utf-8'))

            # Randomly sleep 1 - 3 seconds
            # received micro-batches is going to be a different size
            time.sleep(random.randint(1, 3))

if __name__ == "__main__":

    HOST, PORT = "localhost", 9000
    # Create the server, binding to localhost on port 9000
    with socketserver.TCPServer((HOST, PORT), hwRequestHandler) as server:
        # Activate the server;
        # this will keep running until you interrupt the program with Ctrl-C
        server.serve_forever()